﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class Index : BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HaddleCommon();
            string cookiename = CookiesTools.GetPreUserNameCookie("name");
            string cookiepassword = CookiesTools.GetPreUserPasswordCookie("password");
            string cookieid = CookiesTools.GetPreUserIDCookie("id");
            if (!String.IsNullOrEmpty(cookieid))
            {
                UserData.Id = Int32.Parse(cookieid);
            }
            if (UserData.Id > 0)
            {
                HaddleGet();
            }
            else if (RequestTools.IsPost())
            {
                HaddlePost();
            }
            else
            {
                HaddleGet();
            }
        }



        protected UserInfo _User;
        protected List<NewsInfo> WebNewsList;
        protected List<NewsInfo> BullentinList;
        protected List<UserInfo> ActivestMemebers;
        protected List<DiseaseInfo> DiseaseDataList;
        protected List<UserActionInfo> UserActionList;
        protected List<TopicInfo> TopicDataListHottest;
        protected List<UserInfo> UserDataListLatest;

        /*Begin:added by fsr,20090614*/
        protected List<ActiveUserInfo> RecommendActivestMemebers;
        protected List<ActiveUserInfo> IntroMemebers;
        protected int IntroMemeberUserId = 238;
        protected string IntroMemeberStr = "YOUHP改变了我的观念！在ITP如影随形的很长时间里，我异常沮丧，自怨自艾：为什么是我？......无意中接触到YOUHP，当把自己的病情如实记录的时候，我突然意识到：自己身体上、生理上最不健康的部分尚能为大家带来帮助，更何况我的整个个人、整个人生？！谢谢YOUHP，我会经常来到这里。";
        protected string IntroMemeberName = "李华";
        /*end*/

        protected int n = 10;
        protected int pageSize = 10;
        protected int pageIndex = 1;
        protected int DataCount = 0;

        protected StringBuilder sb = new StringBuilder();

        private void HaddleGet()
        {
            _User = UserBLL.Instance.GetUserInfo(UserData.Id);

            //网站新闻列表,取一页显示
            WebNewsList = NewsInfoBLL.Instance.GetNewsInfoList(true, 9, 1, out DataCount);

            //系统公告列表,取一页显示
            BullentinList = NewsInfoBLL.Instance.GetNewsInfoList(false, 5, 1, out DataCount);

            //网站用户动态(取一天内的全部动态，循环显示)
            UserActionList = UserActionBLL.Instance.GetLatestUserActionList(15, 1, 7, out DataCount);
            Build_HTML();

            //论坛热帖
            TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(10);

            //最新加入用户
            UserDataListLatest = PatientBLL.Instance.GetLastestPatient(4);

            //疾病列表
            DiseaseDataList = DiseaseBLL.Instance.GetHottestDisease(16);

            //指定的活跃用户
            RecommendActivestMemebers = RecommendUserBLL.Instance.ActiveUserGetList();
            IntroMemebers = RecommendUserBLL.Instance.ActiveUserIntroGetList();
            if (IntroMemebers.Count > 0)
            {
                IntroMemeberStr = IntroMemebers[0].Description;
                IntroMemeberUserId = IntroMemebers[0].userid;
                IntroMemeberName = IntroMemebers[0].username;
            }
        }

        private void HaddlePost()
        {
            bool loginsucc = false;
            string userName = RequestTools.GetFormString("username");
            string password = Utils.MD5(RequestTools.GetFormString("password"), 16);
            int expireTime = RequestTools.GetFormInt("expiretime", -1);

            try
            {
                UserData = UserBLL.Instance.CheckUserLogin(userName, password);
                if (UserData.Id > 0)
                {

                    loginsucc = true;
                    //更新登录状态
                    UserBLL.Instance.UpdateUserLoginState(UserData.Id, 1);

                    //CookiesTools.WriteUserCookie(UserData.Id.ToString(),UserData.UserName,expireTime);
                    if (System.Web.HttpContext.Current.Request.Form["expiretime"] == "on")
                    {
                        //CookiesTools.WriteUserInfoCookie(userName, password, expireTime);
                        //CookiesTools.WriteUserNameCookie(userName);
                        //CookiesTools.WriteUserPasswordCookie(password);
                        CookiesTools.WriteUserInfoCookie(userName, password, UserData.Id, 5256000);

                    }
                    CookiesTools.WriteUserCookie(UserData.Id.ToString(), UserData.UserName, expireTime);
                    // userName may be the email, try to verify this!
                    CookiesTools.WriteUserInfoCookie(UserData.UserName, password, UserData.Id, 5256000);
                    this.Redirect("/User/HomePage.aspx");
                }
            }
            catch (Exception e)
            {
                this.AddErrLine(e.ToString());
            }

            if (loginsucc == false)
            {
                this.AddErrLine("登录名或密码错误！");
            }
        }

        private void HaddleCommon()
        {
            this.SetTitle("");
            this.AddLinkCss("/support/alpha/css/Index.css");
        }

        private void Build_HTML()
        {
            foreach (UserActionInfo item in UserActionList)
            {
                // Skip black list user action.
                int userId = item.UserId;
                UserInfo userInfo = UserBLL.Instance.GetUserInfo(userId);
                if (null == userInfo)
                    continue;

                if (0==userInfo.IsActive)
                    continue;

                string TempUrl = "/Profile/Index.aspx?uid=" + item.UserId;
                sb.Append("<li><span style=\"float:right;\"><font color=#838383>" + Utils.GetTimeSpanStr(item.ActionTime) + "</font></span><a href=\"" + this.GetUrl(TempUrl) + "\">" + item.UserName + "</a>&nbsp;");

                switch (item.ActionType)
                {
                    case Constants.ACTION_LOG:
                        item.ActionContent = "写了新日志" + item.ActionContent;
                        break;
                    case Constants.ACTION_TOPIC:
                        item.ActionContent = item.ActionContent;
                        break;
                    case Constants.ACTION_SHARE:
                        item.ActionContent = item.ActionContent;
                        break;
                    case Constants.ACTION_INDICATOR:
                        item.ActionContent = "记录了实验室指标" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_SYMPTOM:
                        item.ActionContent = "记录了症状" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_WEIGHT:
                        item.ActionContent = "记录了体重" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_ADD:
                        item.ActionContent = "开始使用药物" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_STOP:
                        item.ActionContent = "停止使用药物" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_CHANGE:
                        item.ActionContent = "改变了药物的使用剂量" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_EMOTION:
                        item.ActionContent = item.ActionContent;
                        break;
                }
                sb.Append(item.ActionContent + "</li>");

            }
        }


    }
}
